Skip to content

feat(sessions): add offset parameter to get_items() for pagination#2878

Closed
m1lestones wants to merge 1 commit intoopenai:mainfrom
m1lestones:feat/get-items-offset-pagination
Closed

feat(sessions): add offset parameter to get_items() for pagination#2878
m1lestones wants to merge 1 commit intoopenai:mainfrom
m1lestones:feat/get-items-offset-pagination

Conversation

@m1lestones
Copy link
Copy Markdown
Contributor

Summary

Closes #2810

Adds an optional offset: int = 0 parameter to get_items() across all session backends, enabling proper pagination through conversation history.

# Page 1: 10 most recent items (existing behavior, unchanged)
page1 = await session.get_items(limit=10)

# Page 2: next 10 older items
page2 = await session.get_items(limit=10, offset=10)

# Everything except the 5 most recent
older = await session.get_items(offset=5)

Backends updated:

  • SQLiteSessionLIMIT ? OFFSET ? in SQL query
  • AsyncSQLiteSession — same SQL pattern
  • SQLAlchemySession.offset() clause
  • RedisSession — adjusted lrange negative index range
  • DaprSession — list slicing from newest end
  • EncryptSession — passes offset through to underlying session
  • OpenAIResponsesCompactionSession — passes offset through to underlying session
  • OpenAIConversationsSession — fetches limit+offset in DESC, skips first offset, reverses
  • AdvancedSQLiteSession — same SQL pattern, offset appended after branch_id

Test plan

  • Added test_sqlite_session_get_items_with_offset covering offset+limit pagination, offset-only, and out-of-range offset
  • Added test_simple_list_session_get_items_offset for the in-memory test utility
  • Full test suite passes: 2678 passed, 5 skipped
  • make format, make lint, make typecheck all clean
  • Fully backwards compatible — offset defaults to 0, existing behavior unchanged

🤖 Generated with Claude Code

Adds an optional offset: int = 0 parameter to get_items() across all
session backends, enabling callers to paginate backwards through history.

  session.get_items(limit=10)            # 10 most recent (unchanged)
  session.get_items(limit=10, offset=10) # next 10 older items
  session.get_items(offset=5)            # all except 5 most recent

Closes openai#2810

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 20308a3b32

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

async def get_items(
self, limit: int | None = None, offset: int = 0
) -> list[TResponseInputItem]:
encrypted_items = await self.underlying_session.get_items(limit, offset)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Call wrapped get_items with keyword offset

Forwarding limit and offset positionally here breaks wrapped sessions whose second positional parameter is not offset (notably AdvancedSQLiteSession, where it is branch_id) and can also raise TypeError for custom sessions that still accept only limit or make offset keyword-only. This means EncryptedSession.get_items() can silently fetch the wrong history (often empty) even with default arguments; forward using keywords to preserve compatibility.

Useful? React with 👍 / 👎.

async def get_items(
self, limit: int | None = None, offset: int = 0
) -> list[TResponseInputItem]:
return await self.underlying_session.get_items(limit, offset)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Forward offset as keyword to underlying session

This positional forward has the same compatibility regression: wrapped sessions with a non-offset second positional argument (e.g., AdvancedSQLiteSession.branch_id) or older/custom get_items(limit) implementations will misbehave or fail when compaction calls through this path. Because this wrapper is intended to decorate arbitrary Session implementations, offset should be passed by keyword to avoid breaking valid underlying sessions.

Useful? React with 👍 / 👎.

@seratch
Copy link
Copy Markdown
Member

seratch commented Apr 13, 2026

Thanks for sharing this. However, as I mentioned at #2811 (comment), we won't have the optional parameter alone as it won't be used in any use cases.

@seratch seratch closed this Apr 13, 2026
@m1lestones
Copy link
Copy Markdown
Contributor Author

Understood, thank you for the quick feedback. I'll focus on issues with clearer internal use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: add offset parameter to get_items() for real pagination

2 participants